﻿/// <reference path="../devextreme/ts/jquery.d.ts" />

($ => {
    $.fn.reportImageListWrapper = function (options) {
        options = options || {};

        handleGetImages = () => {
            return new Promise(resolve => {
                $.ajax({
                    url: `${options.url}/get-images?reportId=${options.reportId}`,
                    type: 'GET',
                    dataType: 'json',
                    contentType: 'application/json;charset=UTF-8',
                    success: result => {
                        resolve(result.data)
                    },
                    error: () => {
                        resolve([]);
                    }
                })
            });
        };

        handleInsertImages = images => {
            return new Promise((resolve, reject) => {
                $.ajax({
                    url: `${options.url}/insert-images?reportId=${options.reportId}`,
                    type: 'POST',
                    data: JSON.stringify(images.map(image => {
                        return {
                            id: image.id,
                            order: image.order,
                            image: image.data
                        };
                    })),
                    contentType: 'application/json;charset=UTF-8',
                    success: () => {
                        resolve()
                    },
                    error: () => {
                        reject();
                    }
                })
            });
        };

        handleImportImages = () => {
            return new Promise(resolve => {
                $.ajax({
                    url: `${options.url}/${options.reportId}/get-study-base-info`,
                    type: 'GET',
                    dataType: 'json',
                    contentType: 'application/json;charset=UTF-8',
                    success: result => {
                        if (options.importImages instanceof Function && result.data) {
                            options.importImages(result.data).then(data => {
                                resolve(data);
                            });
                        }
                    },
                    error: () => {
                        resolve([]);
                    }
                })
            });
        };

        handleExportImages = images => {
            return new Promise(resolve => {
                if (options.exportImages instanceof Function) {
                    options.exportImages(images).then(() => {
                        resolve();
                    });
                }
            });
        };

        handleInvertImage = image => {
            return new Promise(resolve => {
                if (options.invertImage instanceof Function) {
                    options.invertImage(image).then(data => {
                        resolve(data);
                    });
                }
            });
        };

        handleProcessImages = images => {
            return new Promise(resolve => {
                if (options.processImages instanceof Function) {
                    options.processImages(images).then(data => {
                        resolve(data);
                    });
                }
            });
        };

        handleDeleteImages = images => {
            return new Promise((resolve, reject) => {
                $.ajax({
                    url: `${options.url}/delete-images?reportId=${options.reportId}`,
                    type: 'POST',
                    data: JSON.stringify(images.map(image => image.id)),
                    contentType: 'application/json;charset=UTF-8',
                    success: () => {
                        resolve()
                    },
                    error: () => {
                        reject();
                    }
                })
            });
        };

        handleGetReportImages = () => {
            return new Promise(resolve => {
                $.ajax({
                    url: `${options.url}/get-report-images?reportId=${options.reportId}`,
                    type: 'GET',
                    dataType: 'json',
                    contentType: 'application/json;charset=UTF-8',
                    success: result => {
                        resolve(result.data)
                    },
                    error: () => {
                        resolve([]);
                    }
                })
            });
        };

        handleDeleteReportImages = images => {
            return new Promise((resolve, reject) => {
                $.ajax({
                    url: `${options.url}/delete-report-images?reportId=${options.reportId}`,
                    type: 'POST',
                    data: JSON.stringify(images.map(image => image.id)),
                    contentType: 'application/json;charset=UTF-8',
                    success: () => {
                        resolve()
                    },
                    error: () => {
                        reject();
                    }
                })
            });
        };

        handleClearReportImages = () => {
            return new Promise((resolve, reject) => {
                $.ajax({
                    url: `${options.url}/clear-report-images?reportId=${options.reportId}`,
                    type: 'POST',
                    success: () => {
                        resolve()
                    },
                    error: () => {
                        reject();
                    }
                })
            });
        };

        handleGetReportImagePositions = () => {
            return new Promise((resolve, reject) => {
                $.ajax({
                    url: `${options.url}/get-report-image-positions?reportId=${options.reportId}`,
                    type: 'GET',
                    success: result => {
                        resolve(result.data)
                    },
                    error: () => {
                        reject();
                    }
                })
            });
        };

        handleGetReportImageDescriptions = () => {
            return new Promise((resolve, reject) => {
                $.ajax({
                    url: `${options.url}/get-report-image-descriptions?reportId=${options.reportId}`,
                    type: 'GET',
                    success: result => {
                        resolve(result.data)
                    },
                    error: () => {
                        reject();
                    }
                })
            });
        };

        handleSortReportImage = (imageId, order) => {
            return new Promise((resolve, reject) => {
                $.ajax({
                    url: `${options.url}/sort-report-image?imageId=${imageId}&order=${order}`,
                    type: 'POST',
                    success: () => {
                        resolve()
                    },
                    error: () => {
                        reject();
                    }
                })
            });
        }

        handleUpdateReportImage = (imageId, data) => {
            return new Promise((resolve, reject) => {
                $.ajax({
                    url: `${options.url}/update-report-image?imageId=${imageId}`,
                    type: 'POST',
                    data: {
                        data
                    },
                    success: () => {
                        resolve()
                    },
                    error: () => {
                        reject();
                    }
                })
            });
        }

        handleUpdateReportImageInfo = (imageId, position, description) => {
            return new Promise((resolve, reject) => {
                $.ajax({
                    url: `${options.url}/update-report-image-info?imageId=${imageId}`,
                    type: 'POST',
                    data: {
                        position,
                        description
                    },
                    success: () => {
                        resolve()
                    },
                    error: () => {
                        reject();
                    }
                })
            });
        }

        handleUpdateReportImagePosition = (imageId, position) => {
            return new Promise((resolve, reject) => {
                $.ajax({
                    url: `${options.url}/update-report-image-position?imageId=${imageId}`,
                    type: 'POST',
                    data: {
                        position
                    },
                    success: () => {
                        resolve()
                    },
                    error: () => {
                        reject();
                    }
                })
            });
        }

        handleUpdateReportImageDescription = (imageId, description) => {
            return new Promise((resolve, reject) => {
                $.ajax({
                    url: `${options.url}/update-report-image-description?imageId=${imageId}`,
                    type: 'POST',
                    data: {
                        description
                    },
                    success: () => {
                        resolve()
                    },
                    error: () => {
                        reject();
                    }
                })
            });
        }

        return $(this[0]).reportImageList({
            onGetImages: handleGetImages,
            onInsertImages: handleInsertImages,
            onDeleteImages: handleDeleteImages,
            onGetReportImages: handleGetReportImages,
            onGetReportImagePositions: handleGetReportImagePositions,
            onGetReportImageDescriptions: handleGetReportImageDescriptions,
            onImportImages: handleImportImages,
            onExportImages: handleExportImages,
            onInvertImage: handleInvertImage,
            onProcessImages: handleProcessImages,
            onDeleteReportImages: handleDeleteReportImages,
            onClearReportImages: handleClearReportImages,
            onSortReportImage: handleSortReportImage,
            onUpdateReportImage: handleUpdateReportImage,
            onUpdateReportImageInfo: handleUpdateReportImageInfo,
            onUpdateReportImagePosition: handleUpdateReportImagePosition,
            onUpdateReportImageDescription: handleUpdateReportImageDescription,
            ...options
        });
    }
})(jQuery);